<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <i class="logo"></i>
            <div class="title">智能数据交换云服务平台</div>
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from 'axios';
import {serverUrlNumberPork} from "./api/api.js"
import util from "../libs/util.js"
import {FormatJWT} from "./api/CalebbJWT.1.js"
export default {
    data () {
        return {
            form: {
                userName: 'admin',
                password: '',
                login:''    //判断api访问的数据是否正确
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' },
                    //加用户名不正确
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$store.commit('clearCurrentPath');
            this.$store.commit('clearAllTags');
            this.$router.push({
                name: 'home_index'
            });
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    Cookies.set('user', this.form.userName);
                    Cookies.set('password', this.form.password);
                    // this.$store.commit('setAvator', '../images/userimg/jinyuelogo.png');
                    if (this.form.userName) {
                        Cookies.set('access', 0);
                    } else {
                        Cookies.set('access', 1);
                    }
                    var _this = this;
                    //登录取token
                    this.$jq.ajax({
                        type: 'post',
                        // url: 'http://192.168.0.233:8784'+'/uaa/oauth/token?grant_type=password&username='+_this.form.userName+'&password='+_this.form.password,
                        url: serverUrlNumberPork + '/uaa/oauth/token?grant_type=password&username='+_this.form.userName+'&password='+_this.form.password,
                        headers: {
                            // "Content-Type":"multipart/form-data",
                            'Authorization': 'Basic '+ 'd2ViOnBpZw=='
                        },
                        success: function(res){
                            localStorage.tokenObj = JSON.stringify(res);
                            localStorage.token = res.access_token;
                            localStorage.refresh_token = res.refresh_token;
                            if(localStorage.tokenObj){
                                util.initRouter(_this);
                            }
                            // _this.$router.push({
                            //     name: 'home_index'
                            // });
                        },
                        error: function(res){
                            alert("用户名或密码错误，请输入正确的用户名或密码");
                            Cookies.set('password', "");
                            Cookies.set('user', "");
                            _this.form.password = "";
                        },
                    });
                }
            });
        }
    },
    mounted(){
        let tokenTime = FormatJWT('eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbIioiXSwiYXRpIjoiYjZhYmJhNDQtNjhhZC00ZDI4LTg1OTEtMTEwMGJlNjEyNjdjIiwiVVNFUl9OQU1FIjoiYWRtaW4iLCJleHAiOjE1NDkyODk4NzcsImF1dGhvcml0aWVzIjpbInNoYXJlc2VydmljZTplZGl0IiwiZ2F0aGVyc2VydmVybG9nOmRlbCIsInNoYXJlc2VydmVybG9nOmFkZCIsImdhdGhlcnNlcnZlcmxvZzpnZyIsInJvbGU6YWRkIiwic2hhcmVzZXJ2ZXJsb2c6ZWRpdCIsInNlcnZpY2VyZWdpc3Q6ZGVsIiwiY29ubjphZGQiLCJhbGVydGxvZzphZGQiLCJyb2xlOmRlbCIsImdhdGhlcnNlcnZpY2U6ZGVsIiwic21vcmc6ZGVsIiwiZ2F0aGVyc2VydmljZTplZGl0IiwiYWxlcnRsb2c6ZWRpdCIsInNoYXJlc2VydmljZTpkZWwiLCJyZXNvdXJjZW1hbmFnZTplZGl0Iiwic2VydmljZXJlc291cmNlZGlyOmFkZCIsImdyb3Vwc2VydmljZTplZGl0Iiwib3JnOmFkZCIsIn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vG4W4NRHgNds47BOXNpxTZu81g009WW463RZm7DMUTBd7hTFOqTNXDleo9o0G-VnWYxEGeNMllODvMvnPWmrtGM0tkKhOdUdX_Nm4YCGGZWZ6r7YtthasM8R7Kv-8ZU8No_bzKZbKjSxv7VlFXAnapxxv-enLhiID2AQIWds1EG31jLHoH1ntv4xNBq06JH-43y-enMJVUVWiNvLwwdrKbqwMOqO14earV7cWkS95B1nOu5TAEvaob9Z2qbFAJQBd7aamkOuVr6Ahd_JUzGIJnxb58LoYQV9436CqBOv4Hpw')
        // console.log(tokenTime);
        tokenTime = JSON.parse(tokenTime);
        // console.log(tokenTime.exp);
        let timestamp = Math.round(new Date() / 1000);
        // console.log(timestamp);
        // console.log(tokenTime.exp > timestamp);
    }
};
</script>
